<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%--
  Created by IntelliJ IDEA.
  User: l
  Date: 2022/5/26
  Time: 17:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<!DOCTYPE>
<html>
<head>
    <title>管理后台</title>
    <link href="./css/admin.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="header">
    <div id="welcome">欢迎使用新闻管理系统！</div>
    <div id="nav">
        <div id="logo"><img src="./images/logo.jpg" alt="新闻中国"/></div>
        <div id="a_b01"><img src="./images/a_b01.gif" alt=""/></div>
    </div>
</div>
<div id="admin_bar">

    <div id="status">
        管理员：
        <c:out value="${currentUser.uname}">未登录</c:out>&#160;&#160;&#160;&#160;
        <a href="logout.jsp">login out</a>
        当前在线的有${fn:length(currentUserList)} 个人:
        <c:forEach var="newsUsers" items="${currentUserList}">
            <a href="#">${newsUsers.uname}</a>
        </c:forEach>
    </div>
    <div id="channel"></div>
</div>
<div id="main">
    <div class="opt_area">
        <ul>
            <h3>${news.ntitle}
            </h3>
            <p>
                <span>${news.ncreateDate}</span>
                <span>${news.nauthor}</span>
            </p>
            <div>
                <p>
                    ${news.nsummary}
                </p>
                <p>
                    <img src="${news.npicPath}">
                </p>
                <p>
                    ${news.ncontent}
                </p>
            </div>
            <hr/>
            <div id="commentList">
                <c:if test="${empty commentList}">
                    <p>暂无评论</p>
                </c:if>
                <c:forEach var="comment" items="${commentList}">
                    <li>
                        留言人: ${comment.cauthor}
                        <span>IP: ${comment.cip}</span>
                        <span>留言时间: ${comment.cdate}</span>
                    </li>
                    <li>${comment.ccontent}</li>
                    <hr/>
                </c:forEach>
            </div>
            <c:forEach var="i" begin="1" end="2">
                <br/>
            </c:forEach>
            <script src="js/jquery-3.6.0.js"></script>
            <script>
                $(function () {
                    $("#commentBtn").click(function () {
                        if ($("#ccontent").val() == "") {
                            alert("请输入评论!")
                            return;
                        }
                        let $cnid = $("#cnid").val();
                        let $cauthor = $("#cauthor").val();
                        let $cip = $("#cip").val();
                        let $ccontent = $("#ccontent").val();
                        let $commentList = $("#commentList");
                        $.ajax({
                            url: "InsertOneCommentServlet",
                            type: "Post",
                            data: {"cnid": $cnid, "cauthor": $cauthor, "cip": $cip, "ccontent": $ccontent},
                            dataType: "json",
                            success: function (data) {
                                $commentList.empty();
                                if (data == null || data.length == 0) {
                                    $commentList.append(" <p>暂无评论</p>");
                                }
                                for (let i = 0; i < data.length; i++) {
                                    let $comment = data[i];
                                    let $liComment = $("<li>留言人: "+$comment.cauthor+" <span>IP: "+$comment.cip+"</span> <span>留言时间: "+$comment.cdate+"</span></li><li>"+$comment.ccontent+"</li><hr/>")
                                    $commentList.append($liComment);
                                }
                            },
                            error: function () {
                                alert("读取错误!")
                            }
                        })
                    });
                });
            </script>
            <div>
                <h3>评论</h3>
                <form>
                    <input type="hidden" name="cnid" id="cnid" value="${news.nid}">
                    <p>
                        用户名: <input type="text" name="cauthor" id="cauthor" readonly
                                    value="<c:out value='${currentUser.uname}'>这家伙很懒什么都没留下</c:out>">
                        <span>IP: <input type="text" name="cip" id="cip" readonly value="127.0.0.1"></span>
                    </p>
                    <p>
                        <textarea name="ccontent" id="ccontent" rows="8" cols="50"></textarea>
                    </p>
                    <p>
                        <input type="button" id="commentBtn" value="发表">
                    </p>
                </form>
            </div>
        </ul>
    </div>
</div>
<div id="site_link">
    <a href="">关于我们</a><span>|</span>
    <a href="">Aboue Us</a><span>|</span>
    <a href="">联系我们</a><span>|</span>
    <a href="">广告服务</a><span>|</span>
    <a href="">供稿服务</a><span>|</span>
    <a href="">法律声明</a><span>|</span>
    <a href="">招聘信息</a><span>|</span>
    <a href="">网站地图</a><span>|</span>
    <a href="">留言反馈</a>
</div>
</body>
</html>
